package chapter8.SmallChanageSys;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

//零钱通的过程变成方法
//可以完成收益，消费，查看明细，退出系统等功能
public class SmallChanageSys {
    public static void main(String[] args) {
        boolean loop = true;
        String detail = "";//用来看零钱明细
        double money = 0;//收益金额
        Date date = null;//日期类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm");
        String local = "";
        double comsume = 0;
        double blance = 0;
        do {
            System.out.println("-----零钱通------");
            System.out.println("\t\t1.零钱通明细");
            System.out.println("\t\t2.收益入账");
            System.out.println("\t\t3.消费");
            System.out.println("\t\t4.退\t出");
            System.out.println("请选择1-4");
            Scanner scanner = new Scanner(System.in);
            int mune = scanner.nextInt();
            switch (mune) {
                case 1: {
                    System.out.println("=====零钱通明细====");
                    System.out.println(detail);
                    break;
                }
                case 2: {
                    System.out.println("请输入金额");
                    Scanner scanner1 = new Scanner(System.in);
                    money = scanner1.nextDouble();
                    //也可以选择不正确的条件进行判断直接break
                    if (money > 0) {
                        blance += money;
                        date = new Date();//获取到当前时间
                        detail += "\n收益入账\t" + "+" + money + "\t" + simpleDateFormat.format(date) + "\t" + blance;
                        break;
                    }
                    else {
                        System.out.println("您输入的金额有误，请重新输入");
                        break;
                    }
                }
                case 3: {
                    System.out.println("请输入消费金额");
                    Scanner scanner1 = new Scanner(System.in);
                    comsume = scanner1.nextDouble();
                    System.out.println("请输入消费地点");
                    local = scanner1.next();
                    //提前对余额进行判断
                    //可以选择不对的输入进行判断，然后break;?
                   if((blance -= comsume)>=0){
                    detail += "\n" + local + "\t" + "-" + comsume + "\t" + simpleDateFormat.format(date) + "\t" + blance;
                    break;}
                   else{
                       System.out.println("您的余额不足");
                       break;
                   }
                }
                case 4:
                    String choice = "";
                    while (true) {
                        System.out.println("你真的要退出吗Y/N");
                        Scanner scanner1 = new Scanner(System.in);
                        choice = scanner1.next();
                        if (choice.equals("Y") || choice.equals("N")) {
                            break;
                        }
                    }
                    //当退出while循环后，在对choice进行判断
                    //建议一段代码，完成一个小功能，尽量不要混在一起（低耦合）
                    if (choice.equals("Y")) {
                        loop = false;
                    }
                    break;
                default: {
                    System.out.println("菜单选择有误请重新输入");
                    break;
                }
            }
        } while (loop);
        System.out.println("程序以退出");
    }


}
